<?php

namespace backend\controllers;

use common\cache\DictCache;
use common\helpers\commonApi;
use common\models\Campus;
use common\models\Dis;
use common\models\User;
use common\models\Water;
use Yii;
use yii\helpers\ArrayHelper;

class DisController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = Dis::className();
    }

    public function actionIndex()
    {
        if (Yii::$app->request->isPost){
            $data = parent::actionIndex(true,[['review_status','in',[1,2]]]);
            $campus_ids = array_column($data['data'],'campus_id');
            $campus = (new Campus())->getCampusName($campus_ids);
            $userids = array_unique(ArrayHelper::getColumn($data['data'], 'userid'));
            $userInfo = (new User())->getUserHtml($userids);
            foreach ($data['data'] as &$datum){
                $datum['campus'] = $campus[$datum['campus_id']]??'';
                $datum['user_html'] = $userInfo[$datum['userid']]??'';
            }
            return $data;
        }
        return parent::actionIndex();
    }

    public function actionWait()
    {
        if (Yii::$app->request->isPost){
            $data = parent::actionIndex(true,[['review_status','in',[0]]]);
            $campus_ids = array_column($data['data'],'campus_id');
            $campus = (new Campus())->getCampusName($campus_ids);
            $userids = array_unique(ArrayHelper::getColumn($data['data'], 'userid'));
            $userInfo = (new User())->getUserHtml($userids);
            foreach ($data['data'] as &$datum){
                $datum['campus'] = $campus[$datum['campus_id']]??'';
                $datum['user_html'] = $userInfo[$datum['userid']]??'';
            }
            return $data;
        }
        return parent::actionIndex();
    }

    public function actionReview()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            if (empty($post['id'])){
                return commonApi::message('请选择要审核的人员',false);
            }
            if (empty($post['review_status'])){
                return commonApi::message('请选择审核状态',false);
            }
            $model = Dis::findOne($post['id']);
            if ($model->review_status!==0){
                return commonApi::message('请勿重复审核',false);
            }
            if ($post['review_status']==1){
                $model->status = 1;
            }
            $model->review_status = $post['review_status'];
            $model->review_time = date('Y-m-d H:i:s');
            $model->review_remarks = $post['review_remarks']??'';
            if ($model->save()){
                return commonApi::message('审核成功');
            }
            return commonApi::message('审核失败',false);
        }
        $id = Yii::$app->request->get('id','');
        $model = Dis::findOne($id);
        if (!$model){
            return $this->bError('参数错误');
        }
        $review_status = DictCache::get('review_status');
        $info = $model->toArray();
        $campus = (new Campus())->getCampusName([$info['campus_id']]);
        $info['campus'] = $campus[$info['campus_id']]??'';
        unset($review_status[0]);
        return $this->render('',['model'=>$info,'review_status'=>$review_status]);
    }

    public function actionWater()
    {
        $this->model = Water::className();
        if (Yii::$app->request->isPost){
            return parent::actionIndex(true,[['fund_type','in',[2]]]);
        }
        return parent::actionIndex();
    }
}